package com.smt.modules.app.controller;

import java.util.List;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.common.enums.Code;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.modules.app.annotation.FileSize;
import com.smt.modules.app.annotation.Login;
import com.smt.modules.app.utils.FileSizeUtil;
import com.smt.modules.bus.vo.request.LoadFileReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import com.smt.common.base.BaseController;
import com.smt.modules.bus.vo.AttachmentVo;
import com.smt.modules.bus.entity.AttachmentEntity;
import com.smt.modules.bus.service.AttachmentService;
import com.smt.common.utils.R;



/**
 * 附件表
 *
 * @author murong
 * @email 981620741@qq.com
 * @date 2018-09-29 15:37:09
 */
@RestController
@RequestMapping("app/attachment")
@Api(tags = {"AppAttachmentController"},description = "附件表(客户端接口)")
public class AppAttachmentController extends BaseController{
    @Autowired
    private AttachmentService attachmentService;

    /**
    * 获取当前团队下的文件列表
    * @param project
    * @return
    */
    @GetMapping("/list/{project}")
    @ApiOperation(value  = "获取当前项目下的文件列表",httpMethod = "GET")
    public R list(@PathVariable("project") Long project){

        return attachmentService.list(project);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "/info/{id}",notes = "根据表ID查询",response = AttachmentVo.class)
    public R info(@PathVariable("id") Long id){
			AttachmentEntity attachment = attachmentService.selectById(id);

        return R.ok().put("attachment", attachment);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    @FileSize(operate = "upload")
    @ApiOperation(value = "任务以及子任务上传文件",response = AttachmentVo.class)
    public R save(@RequestBody AttachmentVo attachment,@RequestAttribute("userId")Long userId){
        return attachmentService.save(attachment,userId);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @FileSize(operate = "update")
    @ApiOperation(value =  "修改文件信息",httpMethod = "POST")
    public R update(@RequestBody AttachmentVo attachment){
			attachmentService.updateById(attachment.copyPropertiesTo(new AttachmentEntity()));

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{fileId}")
    @FileSize(operate = "delete")
    @ApiOperation(value = "删除文件",httpMethod = "DELETE")
    public R delete(@PathVariable("fileId")Long fileId){
        AttachmentEntity attachmentEntity = new AttachmentEntity();
        attachmentEntity.setId(fileId);
        attachmentEntity.setIsDelete(1);
        boolean b = attachmentService.updateById(attachmentEntity);
        return b?R.ok("文件删除成功"):
                R.error(EnumToKeyUtil.toInt(Code.DELETE_DATA_ERROR),"文件删除失败");
    }

    /**
     * 测试文件大小
     */
    @PostMapping("/fileSize")
    @ApiOperation(value = "测试文件大小",httpMethod = "POST")
    public R testFile(@RequestParam("fileResource") String fileUrl){
        double fileSize = FileSizeUtil.getFileSizeToNumber(fileUrl);
        return R.ok().put("文件大小",fileSize+"--MB");
    }
    /**
     * 文件下载
     */
    @PostMapping("/download")
    @ApiOperation(value = "任务或者子任务下的文件下载",httpMethod = "POST")
    public R download(@RequestBody LoadFileReq loadFileReq){
        Long taskId = loadFileReq.getTaskId();
        Long subTaskId = loadFileReq.getSubTaskId();
        EntityWrapper<AttachmentEntity> entityWrapper = new EntityWrapper<>();
        if(subTaskId != null){
            //子任务id
            entityWrapper.eq("project_sub_task_id",subTaskId);
        }else if(taskId != null){
            //任务id
            entityWrapper.eq("project_task_id",taskId);
        }
        List<AttachmentEntity> attachmentEntities = attachmentService.selectList(
                entityWrapper.orderBy("create_time",false));
        if(attachmentEntities.size() != 0) {
            return R.ok().put("fileUrl", attachmentEntities.get(0).getUrl()).put("have",1);
        }else{
            return R.ok().put("fileUrl","").put("have",0);
        }
    }
}
